package com.android.lazylist;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.interfata.InterfActivity;
import com.android.interfata.Product1;
import com.android.interfata.R;

public class LazyAdapterSearch extends BaseAdapter
{
    
    private Activity activity;

	public String[] title;
	public String[] detail;	
	public String[] imgid; 	
	public Double[] rating, price;	
	public String[] authors, brand;	
	public Long[] id;	
	public String[] imageLarge;	
	public String[] linkAmazon, linkEbay; 
	
	public int scrHeight, scrWidth;
	
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    public LazyAdapterSearch(Activity a, String[] img, String[] titl, String[] det, Double[]rat, Double[] prc, String[] imgL, 
    		String[] linkA, String[] linkE, Long[] d, String[] aut, String[] brd, int sH, int sW) 
    {
        activity = a;
        
        imgid = img;
        title = titl;
        detail = det;
        rating = rat;
        price = prc;
        imageLarge = imgL;
        linkAmazon = linkA;
        linkEbay = linkE;
        id = d;
        authors = aut;
        brand = brd;
        
        
        
        scrHeight = sH;
        scrWidth = sW; 
        
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() 
    {
        return imgid.length;
    }

    public Object getItem(int position) 
    {
        return position;
    }

    public long getItemId(int position) 
    {
        return position;
    }
    
    
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_s, null);
        
        if (title[position].equals("-"))
        {
        	TextView text=(TextView)vi.findViewById(R.id.title_s);
	        text.setText("You have no items!");
	     
	        
	        ImageView image=(ImageView)vi.findViewById(R.id.img_s);
	        Drawable img = (Drawable) activity.getResources().getDrawable(R.drawable.sad_face);
	        image.setImageDrawable(img);
        }
        else
        {
	        TextView text=(TextView)vi.findViewById(R.id.title_s);
	        text.setText(title[position]);
	        text.setOnClickListener(new Button.OnClickListener() 
			{
	            public void onClick(View v) 
	            {
	           	 	startProductActivity(position);
	            }
	        });
	        
	        TextView det = (TextView)vi.findViewById(R.id.detail_s);
	        det.setText(detail[position]);
	        det.setOnClickListener(new Button.OnClickListener() 
			{
	            public void onClick(View v) 
	            {
	           	 	startProductActivity(position);
	            }
	        });
	        
	     
	        
	        ImageView image=(ImageView)vi.findViewById(R.id.img_s);
	        image.setOnClickListener(new Button.OnClickListener() 
			{
	            public void onClick(View v) 
	            {
	           	 	startProductActivity(position);
	           	 	
	            }
	        });        
	        imageLoader.DisplayImage(imgid[position], image, scrHeight, scrWidth);
        }
        return vi;
    }
    
        
    
    
	public void startProductActivity(int position)
	{
		// Launching new Activity on selecting single List Item
		Intent i = new Intent(activity.getApplicationContext(), Product1.class);
		// sending data to new activity

		i.putExtra("product", title[position]);
		i.putExtra("category", detail[position]);
		i.putExtra("rating", rating[position]);
		i.putExtra("price", price[position]);
		i.putExtra("authors", authors[position]);
		i.putExtra("linkAmazon", linkAmazon[position]);
		i.putExtra("linkEbay", linkEbay[position]);
		i.putExtra("id", id[position]);
		i.putExtra("brand", brand[position]);
		i.putExtra("imageLarge", imageLarge[position]);
		
		activity.startActivity(i);
	}
}
